package com.chen.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.markdown4j.Markdown4jProcessor;

import com.chen.adapter.ListAdapter;
import com.chen.sql.Question;

import gen.R;
import gui.Activity;
import gui.framework.intent.Bundle;
import gui.framework.intent.Intent;
import gui.sql.Count;
import gui.sql.DBHelper;
import gui.sql.DBQuery;
import gui.view.Button;
import gui.view.EditText;
import gui.view.EditText.OnTextChangedListener;
import gui.view.ListView;
import gui.view.ListView.OnItemClickListener;
import gui.view.View;
import gui.view.View.OnClickListener;
import gui.view.WebView;

public class ShowActivity extends Activity {
	private EditText et;
	private ListView lv;
	private ListAdapter adapter;
	private int page = 0;
	private WebView webView;
	private Markdown4jProcessor process;
	private Button pre, next, edit;
	private String key = "";
	private int position;
	private int lastId;
	private LinkedList<Integer> lastStack;
	private final int size = 25;
	private int iwant;
	private int pageCount;
	private boolean isEdit;

	public ShowActivity() {
		setDebug(false);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_activity);
		init();
		findView();
		addListener();
		loadData();
	}

	private void loadData() {
		if (key.trim().length() == 0) {
			List<Question> list = DBHelper.queryEntityList(Question.class,
					"select * from question order by lookcounter desc limit ?,?", page * size, size);
			adapter.setData(list);
		} else {
			String tag = key;
			DBQuery query = new DBQuery(100, lastId);
			adapter.setData(new ArrayList<>());
			query.search(iwant = size, Question.class, new DBQuery.DBQueryCallback<Question>() {

				@Override
				public int nextBlock(List<Question> result) {
					adapter.addData(result);
					return tag != key ? 0 : (iwant -= result.size());
				}

				@Override
				public void stop() {
					if (adapter.getCount() > 0) {
						Question data = adapter.getData(adapter.getCount() - 1);
						lastStack.addFirst(lastId);
						lastId = data.getId();
					}
				}
			}, new String[] { "title", "context" }, key, false);
		}
	}

	private void init() {
		adapter = new ListAdapter();
		process = new Markdown4jProcessor();
		lastStack = new LinkedList<>();
		pageCount = DBHelper.queryEntityList(Count.class, "select count(*) count from question").get(0).getCount();
		pageCount /= size;
	}

	private void addListener() {
		et.setOnTextChangedListener(new OnTextChangedListener() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!s.toString().trim().equals(key)) {
					key = s.toString();
					page = 0;
					lastId = 0;
					lastStack = new LinkedList<>();
					loadData();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(EditText s) {

			}
		});
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(int position, long id) {
				ShowActivity.this.position = position;
				try {
					updateLooker(id);
					String html = process.process(adapter.getData(position).getContext());
					webView.setText(html);
				} catch (IOException e) {
				}
			}
		});
		pre.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (page > 0) {
					page--;
					if (lastStack.size() > 1) {
						lastStack.removeFirst();
						lastId = lastStack.removeFirst();
					}
					loadData();
				}
			}
		});
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (page < pageCount) {
					page++;
					loadData();
				}
			}
		});
		edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				isEdit = true;
				Intent intent = new Intent("gui.intent.action.edit");
				Question data = adapter.getData(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable("question", data);
				intent.putExtra(bundle);
				startActivity(intent);
				onFinish();
			}
		});
	}

	protected void updateLooker(long id) {
		new Thread() {
			public void run() {
				DBHelper.update("update question set lookcounter=lookcounter+1 where id=?", id);
			};
		}.start();
	}

	private void findView() {
		et = (EditText) findViewById(R.id.search);
		lv = (ListView) findViewById(R.id.title_list);
		webView = (WebView) findViewById(R.id.webview_show_activity);
		pre = (Button) findViewById(R.id.pre_show_activity);
		next = (Button) findViewById(R.id.next_show_activity);
		edit = (Button) findViewById(R.id.edit_show_activity);
	}

	@Override
	protected void onDestory() {
		if (!isEdit)
			startActivity(MainActivity.class);
		else {
			isEdit = false;
		}
	}
}
